package cn.net.xyan.easy.webapp.base.controller;

import cn.net.xyan.easy.spring.core.exception.StatusAndMessageError;
import cn.net.xyan.easy.spring.web.annotation.RestServiceConfig;
import cn.net.xyan.easy.webapp.base.annotation.Module;
import cn.net.xyan.easy.webapp.base.api.dto.CommitRegisterUserReq;
import cn.net.xyan.easy.webapp.base.api.dto.QueryWebModuleInfoRsq;
import cn.net.xyan.easy.webapp.base.dao.PermissionGroupDao;
import cn.net.xyan.easy.webapp.base.api.dto.CommitUpdateUserReq;
import cn.net.xyan.easy.webapp.base.api.dto.QueryUserRsq;
import cn.net.xyan.easy.webapp.base.entity.Permission;
import cn.net.xyan.easy.webapp.base.entity.PermissionGroup;
import cn.net.xyan.easy.webapp.base.entity.SystemUser;
import cn.net.xyan.easy.webapp.base.entity.status.PermissionGroupStatus;
import cn.net.xyan.easy.webapp.base.frontend.ui.UserView;
import cn.net.xyan.easy.webapp.base.service.IDTOService;
import cn.net.xyan.easy.webapp.base.service.ISecurityService;
import cn.net.xyan.easy.webapp.base.service.ISystemConfigService;
import cn.net.xyan.easy.webapp.base.service.IWebModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.Arrays;

import java.util.List;

/**
 * Created by xiashenpin on 16/2/9.
 */
@Controller
@RequestMapping(value = "/system")
@Module(
        title = "System Config",
        requiredPermissions = {SystemConfigController.SystemConfigPermission}
)
@RestServiceConfig(packageName = "cn.net.xyan.easy.webapp.base.frontend.service")
public class SystemConfigController {

    public static final String SystemConfigPermission = "SYSTEM";
    public static final String UserConfigPermission = SystemConfigPermission+"_"+"USER";
    public static final String PermissionConfigPermission = SystemConfigPermission+"_"+"PERMISSION";
    public static final String ModuleConfigPermission = SystemConfigPermission+"_"+"MODULE";

    @Autowired
    ISecurityService securityService;

    @Autowired
    ISystemConfigService systemConfigService;

    @Autowired
    PermissionGroupDao permissionGroupDao;

    @Autowired
    IDTOService dtoService;

    @PostConstruct
    public void setup(){
        Permission p1 = securityService.setupPermission(SystemConfigPermission,"System config view");
        Permission p2 = securityService.setupPermission(UserConfigPermission,"User config view");
        Permission p3 = securityService.setupPermission(PermissionConfigPermission,"Permission config view");
        Permission p4 = securityService.setupPermission(ModuleConfigPermission,"Module config view");
        PermissionGroup group = securityService.queryPermissionGroupByGroupName(SystemConfigPermission);
        if (group == null){
            PermissionGroupStatus status = systemConfigService.readStatus(PermissionGroupStatus.class,ISecurityService.ActiveStatus);
            group = new PermissionGroup();
            group.setGroupName(SystemConfigPermission);
            group.setStatus(status);
            group.setDescribe("System config");
            group.getPermissions().addAll(Arrays.asList(p1,p2,p3,p4));

            group = permissionGroupDao.saveAndFlush(group);
        }
    }

    @RequestMapping(value = "/userModule",produces = MediaType.APPLICATION_JSON_VALUE,method = {RequestMethod.POST})
    @Module(title = "User Config",requiredPermissions = {UserConfigPermission})
    public @ResponseBody QueryWebModuleInfoRsq userPage(@RequestHeader(value = "client",required = false) String clientType){
        if (clientType == null) clientType = IWebModuleService.GWT;

        if (IWebModuleService.GWT.equals(clientType)){
            return new QueryWebModuleInfoRsq(UserView.class.getName());
        }else if (IWebModuleService.JSP.equals(clientType)){
            return new QueryWebModuleInfoRsq("");
        }else{
            throw new StatusAndMessageError(-99,"unknow client:"+clientType);
        }
        //QueryWebModuleInfoRsq moduleInfoRsq = new QueryWebModuleInfoRsq("");
        //return moduleInfoRsq;
    }

    @RequestMapping(value = "/users",
           // consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE,
            method = {RequestMethod.POST})
    public @ResponseBody
    Page<QueryUserRsq> allUsers(Pageable pageable){
        Page<SystemUser> users = securityService.allUsers(pageable);

        return dtoService.convert(QueryUserRsq.class,users);
    }

    @RequestMapping(value = "/addUser",
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE,
            method = {RequestMethod.POST})
    public @ResponseBody QueryUserRsq addUser(@RequestBody CommitRegisterUserReq req){
        SystemUser user = securityService.registerUser(req);
        return dtoService.convert(QueryUserRsq.class,user);
    }

    @RequestMapping(value = "/user",
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE,
            method = {RequestMethod.POST})
    public @ResponseBody QueryUserRsq updateUser(@RequestBody CommitUpdateUserReq req){
        SystemUser user = securityService.updateUser(req);
        return dtoService.convert(QueryUserRsq.class,user);
    }

    @RequestMapping(value = "/permissionModule",produces = MediaType.APPLICATION_JSON_VALUE,method = {RequestMethod.POST})
    @Module(title = "Permission Config",requiredPermissions = {PermissionConfigPermission})
    public @ResponseBody QueryWebModuleInfoRsq permissionPage(@RequestHeader(value = "client",required = false) String clientType){
        QueryWebModuleInfoRsq moduleInfoRsq = new QueryWebModuleInfoRsq("");
        return moduleInfoRsq;
    }


}
